package com.health.httpbean.format;

import android.text.TextUtils;

import com.blankj.utilcode.util.TimeUtils;
import com.health.db.FDbManager;
import com.health.httpbean.QueryTypeEnum;
import com.health.httpbean.model.DiabetesBean;
import com.health.httpbean.model.MedicineBean;
import com.health.httpbean.modelnew.FaPersonnelDiabetesForm;
import com.health.httpbean.modelnew.FaPersonnelHypertensionForm;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tools.ImageTools;
import com.health.viewscontrols.NovaApplication;

import net.tsz.afinal.db.sqlite.DbModel;

import java.util.ArrayList;
import java.util.List;

import static com.health.httpbean.format.FormatUtils.formatAskSymptom;
import static com.health.httpbean.format.FormatUtils.parseDouble;
import static com.health.httpbean.format.FormatUtils.parseInt;
import static com.health.httpbean.format.FormatUtils.parseString;

/**
 * author : xiaoxin
 * date   : 2020/12/17 9:25
 * desc   : 糖尿病
 * update :
 */
public class FormatTnbData {

    public static DiabetesBean formatGxy(CD_DIABETESFOLLOWUP table, List<BD_DD_INFO> listZZs) {
        DiabetesBean bean = new DiabetesBean();
//        bean.ID_UserDocument = table.getARCHIVEID();
        try {
            bean.UserName = table.getCUSTOMERNAME();
            bean.BianHao = table.getCUSTOMERID();
            try {
                bean.SuiFangRiQi = TimeUtils.millis2String(
                        table.getFOLLOWUP_DATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }
            //有无转诊   DM01-11_2 有 DM01-11_1 无
            bean.ZhuanZhenQingKuang = table.getISREFERRAL() == "DM01-11_2" ? 2 : 1;
            bean.ZhuanZhenYuanYin = table.getREFERRALREASON();
            bean.ZhuanZhenJiGou = table.getREFERRALORG();
            try {
                bean.XiaCiSuiFangRiQi = TimeUtils.millis2String(
                        table.getNEXTFOLLOWUP_DATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }

//        bean.SuiFangYiSheng = table.getFOLLOWUPDOCTOR();
            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + table.getFOLLOWUPDOCTOR() + "'";
            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
            bean.SuiFangDoctorName = dbSql.getString("NAME");

            //随访类型、随访分类 :1门诊 2家庭 3电话 哪个是  是不是这里接口多了一个字段
            //DM04-15_1 解析出错
            bean.SuiFangLeiXing = parseInt(table.getFOLLOWUPWAY());
            bean.SuiFangFangShi = parseInt(table.getFOLLOWUPWAY());

//        bean.ID_FollowUPTable = parseInt(table.getFOLLOWUPWAY());

//        bean.TNBID = table.getID();
            bean.TNBZhengZhuang = formatAskSymptom(table.getSYMPTOM(),listZZs);
            bean.TNBGaoYa = parseDouble(table.getHYPERTENSION());
            bean.TNBDiYa = parseDouble(table.getHYPOTENSION());
            bean.TNBShenGao = parseDouble(table.getHeight());
            bean.TNBTiZhong = parseDouble(table.getWEIGHT());
            bean.TNBMuBiaoTiZhong = parseDouble(table.getWEIGHTTARGET());
            bean.TNBTiZhiZhiShu = parseDouble(table.getBMI());
            bean.TNBMuBiaoTiZhiZhiShu = parseDouble(table.getBMITARGET());
            bean.TNBZuBeiDongMaiBoDong = parseInt(table.getDORSALISPEDISPULSE());
//        bean.TNBZuBeiBuWei = parseInt(table.getDORSALISPEDISPULSE());
            bean.TNBQiTa = parseString(table.getPHYSICALSYMPTOMOTHER());
            bean.TNBRiXiYanLiang = parseInt(table.getDAILYSMOKENUM());
            bean.TNBMuBiaoRiXiYanLiang = parseInt(table.getDAILYSMOKENUMTARGET());
            bean.TNBRiYinJiuLiang = parseInt(table.getDAILYDRINKNUM());
            bean.TNBMuBiaoRiYinJiuLiang = parseInt(table.getDAILYDRINKNUMTARGET());
            bean.TNBYunDongPinLv = parseInt(table.getSPORTTIMEPERWEEK());
            bean.TNBYunDongShiChang = parseInt(table.getSPORTPERMINUTETIME());
            bean.TNBMuBiaoYunDongPinLv = parseInt(table.getSPORTTIMESPERWEEKTARGET());
            bean.TNBMuBiaoYunDongShiChang = parseInt(table.getSPORTPERMINUTESTIMETARGET());
            bean.TNBZhuShi = parseInt(table.getSTAPLEFOODDAILYG());
            bean.TNBMuBiaoZhuShi = parseInt(table.getSTAPLEFOODDAILYGTARGET());
            bean.TNBXinLiTiaoZheng = parseInt(table.getPSYCHOADJUSTMENT());
            bean.TNBZunYiXingWei = parseInt(table.getOBEYDOCTORBEHAVIOR());
            bean.TNBKongFuXueTangZhi = parseDouble(table.getFPG());
            bean.TNBTangHuaXueHongDanBai = parseDouble(table.getHBALC());
            try {
                bean.TNBJianChaShiJian = TimeUtils.millis2String(
                        table.getEXAMDATE().getTime(), "yyyy-MM-dd");
            } catch (Exception e) {
                e.printStackTrace();
            }

            bean.TNBYaoWuYiCongXing = parseInt(table.getMEDICATIONCOMPLIANCE());
            bean.TNBYaoWuBuLiangFanYingQingKuang = parseInt(table.getADR());
            bean.TNBYaoWuBuLiangFanYingMiaoShu = parseString(table.getADR_EX());
            bean.TNBDiXueTangFanYing = parseInt(table.getHYPOGLYCEMIARREACTION());
            bean.TNBCiCiSuiFangFenLei = parseInt(table.getFOLLOWUPTYPE());
            bean.TNBZhengZhuangQiTa = parseString(table.getSYMPTOMOTHER());

            String strWhere = "IDCARD = '" + table.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere + "AND TYPE = '2'");

            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
            List<MedicineBean> listMedicine = new ArrayList<>();
            for (int i = 0; i < drugList.size(); i++) {
                if (i == 3) {
    //                bean.GXYYaoWuMingChengQiTa = drugList.get(i).getNAME();
    //                bean.GXYMeiRiCiShuQiTa = parseInt(drugList.get(i).getDAILYTIME());
    //                bean.GXYMeiCiShuLiangQiTa = drugList.get(i).getEVERYTIMEMG();
                } else {
                    MedicineBean medicineBean = new MedicineBean();
                    medicineBean.MedicineName = drugList.get(i).getNAME();
                    medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
                    for (int j = 0; j < unitList.size(); j++) {
                        if (drugList.get(i).getRES1() != null
                                && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
                            medicineBean.Unit = unitList.get(j).getDATAINFO();
                        }
                    }

                    String dailytime = drugList.get(i).getDAILYTIME();
                    if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                        String[] split = dailytime.split("/");
                        if(null != split && !split.equals("")){
                            String s = split[1];
                            if(null != s && !s.equals("")){
                                if(s.equals("99")){
                                    medicineBean.Method = FormatUtils.drugMethod(23);
                                }else{
                                    medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                                }
                            }
                            String s1 = split[0];
                            if(null != s1 && !s1.equals("")){
                                if(s1.equals("99")){
                                    medicineBean.Cycle = FormatUtils.cycle(14);
                                }else{
                                    medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                                }
                            }
                        }

                    }
                    listMedicine.add(medicineBean);
                }
            }

            bean.listMedicine = listMedicine;

            bean.TNBYiDaoSuZhongLei = parseString(table.getINSULINTYPE());
            bean.TNBYiDaoSuYongLiang = parseString(table.getINSULINUSAGE());
            bean.TNBYiDaoSuYongFa = parseString(table.getINSULINUSAGE());
            bean.CardID = table.getIDCARD();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return bean;
    }

    public static FaPersonnelDiabetesForm formatGxyNew(CD_DIABETESFOLLOWUP gxyModel, List<BD_DD_INFO> list) {

        FaPersonnelDiabetesForm form = new FaPersonnelDiabetesForm();

        try {
            form.sjType = QueryTypeEnum.TNB.getCode();
            form.idCard = gxyModel.getIDCARD();
            form.archivesName = gxyModel.getCUSTOMERNAME();
            form.number = gxyModel.getCUSTOMERID();

            String sql = "SELECT * FROM BD_ORGANIZE_EMPLOYEE WHERE USER_ID = '" + gxyModel.getFOLLOWUPDOCTOR() + "'";
            DbModel dbSql = NovaApplication.fdbmanger.findDbSql(sql);
            form.doctorName = dbSql.getString("NAME");
            form.doctorCardId = dbSql.getString("IDCARD");
            form.followTime = gxyModel.getFOLLOWUP_DATE();
            form.followType = parseInt(gxyModel.getFOLLOWUPWAY());
            form.symptom = formatAskSymptom(gxyModel.getSYMPTOM(),list);
            form.otherSymptom = parseString(gxyModel.getSYMPTOMOTHER());
            form.bloodPressure = gxyModel.getHYPERTENSION() + "/" + gxyModel.getHYPOTENSION();
            form.weight = gxyModel.getWEIGHT();
            form.targetWeight = gxyModel.getWEIGHTTARGET();
            form.bmi = gxyModel.getBMI();
            form.targetBmi = gxyModel.getBMITARGET();
            form.dorsalisPedisPulsation = parseInt(gxyModel.getDORSALISPEDISPULSE());
            form.otherSign = gxyModel.getPHYSICALSYMPTOMOTHER();
            form.dailySmoking = gxyModel.getDAILYSMOKENUM();
            form.targetDailySmoking = gxyModel.getDAILYSMOKENUMTARGET();
            form.dailyDrinking = gxyModel.getDAILYDRINKNUM();
            form.targetDailyDrinking = gxyModel.getDAILYDRINKNUMTARGET();
            form.weeklyExerciseFrequency = gxyModel.getSPORTTIMEPERWEEK();
            form.weeklyExerciseFrequencyT = gxyModel.getSPORTPERMINUTETIME();
            form.targetWeeklyExerciseFrequency = gxyModel.getSPORTTIMESPERWEEKTARGET();
            form.targetWeeklyExerciseFrequencyT = gxyModel.getSPORTPERMINUTESTIMETARGET();

            form.stapleFood = gxyModel.getSTAPLEFOODDAILYG();
            form.targetStapleFood = gxyModel.getSTAPLEFOODDAILYGTARGET();


            form.mindAdjustment = parseInt(gxyModel.getPSYCHOADJUSTMENT());
            form.complianceBehavior = parseInt(gxyModel.getOBEYDOCTORBEHAVIOR());
            form.mindAdjustment = parseInt(gxyModel.getPSYCHOADJUSTMENT());
            form.complianceBehavior = parseInt(gxyModel.getOBEYDOCTORBEHAVIOR());
            form.accessoryExamination = gxyModel.getFPG();
            form.saccharification = gxyModel.getHBALC();
            form.auxiliaryChecks = gxyModel.getEXAMDATE();
            form.rhg = parseInt(gxyModel.getHYPOGLYCEMIARREACTION());
            form.dorsalisPePulPosition = parseInt(gxyModel.getHYPOGLYCEMIARREACTION());
            if ("DM01-72_2".equals(gxyModel.getDORSALISPEDISPULSE())) {
                form.dorsalisPePulPosition = parseInt(gxyModel.getDORSALISPEDISPULSE_Less());
            }else if ("DM01-72_3".equals(gxyModel.getDORSALISPEDISPULSE())) {
                form.dorsalisPePulPosition = parseInt(gxyModel.getDORSALISPEDISPULSE_Disa());
            }
            form.medicationCompliance = parseInt(gxyModel.getMEDICATIONCOMPLIANCE());
            form.drugAdverseReaction = parseInt(gxyModel.getADR());
            form.drugAdverseReactionText = gxyModel.getADR_EX();
            form.followClassification = parseInt(gxyModel.getFOLLOWUPTYPE());
            form.referralReason = gxyModel.getREFERRALREASON();
            form.referralInfo = gxyModel.getREFERRALREASON();
            form.referralDepartment = gxyModel.getREFERRALORG();
            form.nextTime =  gxyModel.getNEXTFOLLOWUP_DATE();
            form.createtime =  gxyModel.getCREATED_DATE();

            form.insulinType = parseString(gxyModel.getINSULINTYPE());
            form.currentInsulin = parseString(gxyModel.getINSULINTYPE());
            form.currentMedication = parseString(gxyModel.getINSULINTYPE());
            form.height =  gxyModel.getHeight();


            StringBuilder drugListStr = new StringBuilder();
            String strWhere = "IDCARD = '" + gxyModel.getIDCARD() + "'";
            List<CD_DRUGCONDITION> drugList = NovaApplication.fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                    strWhere + "AND TYPE = '2'");

            List<BD_DD_INFO> unitList = FDbManager.getInstance(NovaApplication.getInstance())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class,
                            "PARENTID='DM04-05'" + " order by cast(SORT as int) asc");
            List<MedicineBean> listMedicine = new ArrayList<>();
            for (int i = 0; i < drugList.size(); i++) {
                if (i == 3) {
                    //                bean.GXYYaoWuMingChengQiTa = drugList.get(i).getNAME();
                    //                bean.GXYMeiRiCiShuQiTa = parseInt(drugList.get(i).getDAILYTIME());
                    //                bean.GXYMeiCiShuLiangQiTa = drugList.get(i).getEVERYTIMEMG();
                } else {
                    MedicineBean medicineBean = new MedicineBean();
                    medicineBean.MedicineName = drugList.get(i).getNAME();
                    medicineBean.Dosage = drugList.get(i).getEVERYTIMEMG();
                    for (int j = 0; j < unitList.size(); j++) {
                        if (drugList.get(i).getRES1() != null
                                && drugList.get(i).getRES1().equals(unitList.get(j).getID())) {
                            medicineBean.Unit = unitList.get(j).getDATAINFO();
                        }
                    }

                    String dailytime = drugList.get(i).getDAILYTIME();
                    if(null != dailytime && !dailytime.equals("") && dailytime.contains("/")){
                        String[] split = dailytime.split("/");
                        if(null != split && !split.equals("")){
                            String s = split[1];
                            if(null != s && !s.equals("")){
                                if(s.equals("99")){
                                    medicineBean.Method = FormatUtils.drugMethod(23);
                                }else{
                                    medicineBean.Method = FormatUtils.drugMethod(Integer.parseInt(s)-1);
                                }
                            }
                            String s1 = split[0];
                            if(null != s1 && !s1.equals("")){
                                if(s1.equals("99")){
                                    medicineBean.Cycle = FormatUtils.cycle(14);
                                }else{
                                    medicineBean.Cycle = FormatUtils.cycle(Integer.parseInt(s1)-1);
                                }
                            }
                        }

                    }
                    listMedicine.add(medicineBean);
                    //1 维生素C 一日两次 每次1mg 口服;2 维生素C 一日两次 每次1mg 口服
                    drugListStr.append((i + 1) + ". " + drugList.get(i).getNAME()
                            + " " + medicineBean.Cycle
                            + " 每次" + drugList.get(i).getEVERYTIMEMG() + medicineBean.Unit
                            + " " + medicineBean.Method + ";"
                    );
                }
            }

            form.medication = drugListStr.toString();
            form.currentMedication = drugListStr.toString();
            form.res1 = gxyModel.getASSISTANTEXAM();

            if (!TextUtils.isEmpty(gxyModel.getRES4())) {
                String base64 = ImageTools.imageToBase64(gxyModel.getRES4());
                form.picBase64 = "data:image/png;base64," + base64;
            }
//            highBloodBean.listMedicine = listMedicine;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return form;
    }
}
